﻿
Settings:
    gui dbset:add,text,,Set CollectPtah
    gui dbset:Add,Edit,vCollectPtah gWrite,
    gui dbset:add,text,,Set EditorPath
    gui dbset:Add,Edit,vEditorPath gWrite,
    Gui dbset:show
Return

Write:
    gui,Submit,NoHide
    db[settings,"CollectPtah"]:=CollectPtah
    db.save()
Return

~^c::DoublePress("MeunGui")
MeunGui(){
    CoordMode,Mouse
    DetectHiddenWindows,on
    初始=
    (
        [settings]
        CollectPtah=C:\Users\Administrator\Desktop\Collect\
        EditorPath=C:\SoftDir\VSCode-win32-x64-1.70.0\Code.exe
    )
    IfNotExist,DoubleCopyMenu.ini
    {
        db:= class_EasyIni("DoubleCopyMenu.ini",初始)
        db.save()
    }
    db:= class_EasyIni("DoubleCopyMenu.ini")
    for k,v in db["settings"]
    {
        %k%=%v%
    }
    Global FileName, SaveEdit, Allstr
    Allstr := Clipboard
    CoordMode, Mouse, Screen
    MouseGetPos, Mousex, Mousey
    Gui dcmain: Destroy
    Gui dcmain: +AlwaysOnTop +HwndSaveGui -Caption
    Gui dcmain: Font, s12
    Gui dcmain: Add, Edit, x10 y10 w220 vFileName HwndSaveEdit,
    Gui dcmain: Font, s12
    Gui dcmain: Add, button, x10 w100 gGetSelect, 1GetSelect
    Gui dcmain: Add, button, x+5 w100 gSaveAHK, 2SaveAHK 
    Gui dcmain: Add, button, x+5 w100 gSaveTxt, 3SaveTxt
    Gui dcmain: Add, button, x10 w100 gRunAhk, 4RunAhk
    Gui dcmain: Add, button, x+5 w100 gSettings,5Settings
    Gui dcmain: Add, button, x+5 w100 g_CloseTempAhk, 6CloseTempAhk
    Gui dcmain: Add, button, x10 w100 gAhkHelp, AhkHelp 
    Gui dcmain: Add, button, x+5 w100 gCreateWords,CreateWords
    Gui dcmain: Add, button, x+5 w100 gQuitdc,Quitdc

    Gui dcmain: Show, % "x" Mousex " y"Mousey,DoubleClickMenu
    CoordMode, Mouse
    ControlFocus, , ahk_id %SaveEdit%
    OnMessage(0x201, "MoveGui")
    OnMessage(0x200, "GuiTips")
    创建快捷键()
}
GuiEscape:
GuiClose:
    Quitdc()

    MoveGui(){
        PostMessage, 0xA1, 2
    }

AhkHelp:
    Loop, Read, wd.txt
    {
        If (Trim(Clipboard," ") = RegExReplace(A_LoopReadLine, "[()]*<<>>.+"))
            mbUrl := RegExReplace(A_LoopReadLine, "^.+<<>>")
    }
    If (mbUrl)
    {
        mubiao := "C:\AHK\SciTE\中文帮助\AutoHotkey_CN.chm::" mbUrl
        if (WinExist("ahk_class HH Parent"))
            WinClose, ahk_class HH Parent
        run, "hh.exe" "%mubiao%"
        mbUrl := ""
    }
    Quitdc()
Return

GetSelect(){
    Global SaveEdit
    Clipboard := ""
    Send, ^c
    ClipWait, 1
    ControlSetText, , % Clipboard, ahk_id %SaveEdit%
}
SaveAHK(){
    Global FileName, SaveEdit, Allstr, CollectPtah
    ControlGetText, FileName, , ahk_id %SaveEdit%
    ListName := (FileName = "") ? "Test" : FileName
    FileAppend, % Allstr, % CollectPtah "\" ListName ".ahk", UTF-8
    run % EditorPath CollectPtah "\" ListName ".ahk"
    Quitdc()
}
SaveTxt(){
    Global FileName, SaveEdit, Allstr, CollectPtah
    ControlGetText, FileName, , ahk_id %SaveEdit%
    ListName := (FileName = "") ? "Test" : FileName
    FileAppend, % Allstr, % CollectPtah "\" ListName ".txt", UTF-8
    run % EditorPath CollectPtah "\" ListName ".txt"
    Quitdc()
}
Quitdc(){
    Global Allstr
    Gui dcmain: Destroy
    Allstr := ""

}
_获取首行(){
    Global SaveEdit, Allstr
    Firstline := RegExReplace(RegExReplace(RegExReplace(Allstr, "[\r\n]+", "`n"), "^(.+?\n)(.+$)", "$1"), "[\\/:?*""|<>(){} \r;\n]")
        ControlSetText, , % Firstline, ahk_id %SaveEdit%
    }
    创建快捷键(){
        Global SaveEdit
        Hotkey, IfWinActive,DoubleClickMenu
            hotkey, 1, GetSelect
        hotkey, 2, SaveAHK
        hotkey, 3,SaveTxt
        hotkey, 4,RunAhk
        hotkey, 5,Settings
        hotkey, 6,_CloseTempAhk
        ; hotkey, 6,app
        ; hotkey, 7,option1
        ; hotkey, 8,cikuManager1
        hotkey, c,CreateWords
        hotkey, a,AhkHelp
        hotkey, q,Quitdc

    }
    DoublePress(GoFunc := ""){
        static pressed1 = 0
        if (pressed1 and A_TimeSincePriorHotkey <= 200){
            pressed1 = 0
            SetTimer, %GoFunc%, -10
        }
        else {
            pressed1 = 1
        }
    }
    RunAhk(){
        Global Allstr
        Exec(Allstr, 1)
        return
    }
    _CloseTempAhk(){
        Exec("", 1)
        Gui dcmain: Destroy

    }

    ; ==================================================================================
    ; dbgba 的新建进程 
    ; ==================================================================================
    Exec(s, flag="Default") {
        SetBatchLines, -1
        SetWorkingDir %A_ScriptDir%
        DetectHiddenWindows, On
        WinGet, NewPID, PID, <<ExecNew%flag%>> ahk_class AutoHotkeyGUI
        PostMessage, 0x111, 65307,,, %A_ScriptDir%\* ahk_pid %NewPID%
        add=`nflag=<<ExecNew%flag%>>`n
        (%
            Gui, Gui_Flag_Gui: Show, Hide, %flag%
            DllCall("RegisterShellHookWindow", "Ptr", A_ScriptHwnd)
            OnMessage(DllCall("RegisterWindowMessage", "Str", "ShellHook"), "ShellEvent")
            ShellEvent(wParam, lParam) {
                DetectHiddenWindows, On
                if !WinExist("HostProcessName")
                    ExitApp
            }
        )
        s:=add "`n" s "`nExitApp"
        s:=RegExReplace(s, "HostProcessName", "ahk_pid "DllCall("GetCurrentProcessId"))
        exec := ComObjCreate("WScript.Shell").Exec(A_AhkPath " /ErrorStdOut /f *")
        exec.StdIn.Write(s)
        exec.StdIn.Close()
    }

MsgBox,test